package shop

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"fast-boot/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type CategoryUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改类型
func NewCategoryUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryUpdateLogic {
	return &CategoryUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CategoryUpdateLogic) CategoryUpdate(req *types.PmsCategoryForm) (resp *types.PmsCategoryFormResp, err error) {

	info := model.PmsCategoryModel{}
	l.svcCtx.GormClient.GormDb.First(&info, req.Id)

	if info.Id == 0 {
		logx.WithContext(l.ctx).Errorf("不存在: %s", req.Id)
		return nil, xerr.NewErrMsg("不存在")
	}
	info.ShopId = req.ShopId
	info.ParentId = req.ParentId
	info.Sort = req.Sort
	info.Name = req.Name
	info.Icon = req.Icon
	info.Sort = req.Sort
	info.Visible = req.Visible
	if err = l.svcCtx.GormClient.GormDb.Save(&info).Error; err != nil {
		return resp, err
	}

	return resp, err
}
